// <copyright file="RPGRoguelikeEngineTestApp.Events.cs" company="TenneySoftware">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>Robert Tenney</author>
// <email>rob10e@yahoo.com</email>
// <date>April 2009</date>
// <summary>Main application display form - events portion.</summary>

// License:

// Product: RPGRoguelikeEngine .Net
// Author: Robert N. Tenney
// Copyright: April 2009

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation version 2
// of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// This license covers all source code, binaries, and other materials related to this project
// unless specifically stated otherwise.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TenneySoftware.RPGRoguelikeEngineTestApp {
    /// <summary>
    /// Description of LevelTestForm_Events.
    /// </summary>
    public partial class MainForm {
        /// <summary>
        /// Flag indicating which button was clicked.
        /// </summary>
        private bool leftClicked, rightClicked;
        #region DungeonDisplay Events

        /// <summary>
        /// Called by the Paint event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void DungeonDisplayPaint(object sender, PaintEventArgs e) {
//            this.engine.Update();
        }

        /// <summary>
        /// Called by the MouseMove event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void DungeonDisplayMouseMove(object sender, MouseEventArgs e) {
            this.offset = Point.Empty;
            if (this.panning) {
                this.offset.X = (e.Location.X - this.origin.X);
                this.offset.Y = (e.Location.Y - this.origin.Y);
                this.engine.GraphicsManager.OffsetDisplay(this.offset);
            }

            this.origin = e.Location;
            this.engine.GraphicsManager.SetActiveTile(this.origin, true);
            this.StatusBarLabel.Text = this.engine.GetTile(this.engine.GraphicsManager.ActiveTile).ToString() +
                                       " at location: " + this.engine.GraphicsManager.ActiveTile.ToString();
        }

        /// <summary>
        /// Called by the MouseDown event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void DungeonDisplayMouseDown(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Middle) {
                this.panning = true;
                this.Cursor = Cursors.Hand;
            }

            if (e.Button == MouseButtons.Left) {
                this.leftClicked = true;
            }

            if (e.Button == MouseButtons.Right) {
                this.rightClicked = true;
            }
        }

        /// <summary>
        /// Called by the MouseUp event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void DungeonDisplayMouseUp(object sender, MouseEventArgs e) {
            if (this.panning) {
                this.origin = Point.Empty;
                this.panning = false;
                this.Cursor = Cursors.Default;
            }

            if (this.leftClicked) {
                this.leftClicked = false;
                this.player.CharacterActions.WalkTo(this.engine.GraphicsManager.ActiveTile);
            }

            if (this.rightClicked) {
                this.rightClicked = false;
            }
        }

        /// <summary>
        /// Called by the Resize event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void DungeonDisplayResize(object sender, EventArgs e) {
            this.ResizeIt();
        }

        /// <summary>
        /// Called by the PreviewKeyDown event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void DungeonDisplayPreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
            if (!this.engine.ConsoleModeActive) {
                this.ProcessOtherKeys(e.KeyCode);
            } else {
                this.engine.Console.InputOtherChar(e.KeyValue);
            }
        }
        #endregion

        #region MainForm Events
        /// <summary>
        /// Called by the FormClosing event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MainFormFormClosing(object sender, FormClosingEventArgs e) {
            this.engine.GraphicsManager.Dispose();
        }

        /// <summary>
        /// Called by the FormClosed event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MainFormFormClosed(object sender, FormClosedEventArgs e) {
            this.engine.GraphicsManager.Dispose();
        }
        #endregion

        #region Menu Events
        /// <summary>
        /// Called by the Generator_Click event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void Generator_Click(object sender, EventArgs e) {
            ToolStripMenuItem itemClicked = sender as ToolStripMenuItem;
            string predefinedClicked = itemClicked.Text;
            string generatorClicked = itemClicked.OwnerItem.Text;

            foreach (ToolStripMenuItem item in this.menuLevelGenerators.DropDownItems) {
                item.Checked = false;
                foreach (ToolStripMenuItem subItem in item.DropDownItems) {
                    subItem.Checked = false;
                }
            }

            itemClicked.Checked = true;
            (itemClicked.OwnerItem as ToolStripMenuItem).Checked = true;
            this.GeneratorSelect(generatorClicked, predefinedClicked);
            this.Regenerate();
        }

        /// <summary>
        /// Called by the Manager_Click event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void Manager_Click(object sender, EventArgs e) {
            string text = sender.ToString();
            this.ManagerSelect(text);
        }

        /// <summary>
        /// Called by the MenuFileItemRegenerateClick event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MenuFileItemRegenerateClick(object sender, EventArgs e) {
            this.Regenerate();
        }

        /// <summary>
        /// Called by the MenuFileItemExitClick event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MenuFileItemExitClick(object sender, EventArgs e) {
            Environment.Exit(0);
        }

        /// <summary>
        /// Called by the MenuViewItemLevelClick event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MenuViewItemLevelClick(object sender, EventArgs e) {
            if (!this.menuViewItemLevel.Checked) {
                this.ToggleView();
            }
        }

        /// <summary>
        /// Called by the MenuViewItemMapClick event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MenuViewItemMapClick(object sender, EventArgs e) {
            if (!this.menuViewItemMap.Checked) {
                this.ToggleView();
            }
        }

        /// <summary>
        /// Called by the MenuCenteringModeChanged event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MenuCenteringModeChanged(object sender, EventArgs e) {
            ToolStripItem item = (ToolStripItem)sender;
            this.SetCenteringMode(item.Text);
        }
        #endregion

        /// <summary>
        /// Called by the MainFormShown event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MainFormShown(object sender, EventArgs e) {
            this.Generate();
            this.SetCenteringMode("On Player");
            this.MainLoop();
        }

        /// <summary>
        /// Called by the MainFormKeyPress event.
        /// </summary>
        /// <param name="sender">Sender of the event.</param>
        /// <param name="e">Event arguments.</param>
        private void MainFormKeyPress(object sender, KeyPressEventArgs e) {
            if (!this.engine.ConsoleModeActive) {
                this.ProcessRegularKeys(e.KeyChar);
                e.Handled = true;
            } else {
                if (e.KeyChar == '~') {
                    this.engine.ConsoleModeActive = false;
                    return;
                }

                if (Char.IsLetterOrDigit(e.KeyChar) ||
                        Char.IsPunctuation(e.KeyChar) ||
                        Char.IsSeparator(e.KeyChar)) {
                    this.engine.Console.InputChar(e.KeyChar);
                    e.Handled = true;
                }
            }
        }
    }
}
